package com.cz.ssyx.activity.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cz.ssyx.activity.service.CouponInfoService;
import com.cz.ssyx.common.result.Result;
import com.cz.ssyx.model.activity.CouponInfo;
import com.cz.ssyx.vo.activity.CouponRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.License;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券信息 前端控制器
 * </p>
 *
 * @author Joker
 * @since 2024-04-20
 */
@Api(tags = "优惠券管理")
@RestController
//@CrossOrigin
@RequestMapping("/admin/activity/couponInfo")
public class CouponInfoController {

    @Autowired
    private CouponInfoService couponInfoService;

    @ApiOperation("分页查询优惠券")
    @GetMapping("/{page}/{limit}")
    public Result getPageList(@PathVariable Long page, @PathVariable Long limit) {
        Page<CouponInfo> couponInfoPage = new Page<>(page, limit);
        Page<CouponInfo> couponInfoPageList = couponInfoService.getPageList(couponInfoPage);
        return Result.ok(couponInfoPageList);
    }

    @ApiOperation("根据id获取优惠券基本信息")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id) {
        CouponInfo couponInfo = couponInfoService.getById(id);
        couponInfo.setCouponTypeString(couponInfo.getCouponType().getComment());
        couponInfo.setCouponTypeString(couponInfo.getRangeType().getComment());
        return Result.ok(couponInfo);
    }

    @ApiOperation("新增优惠券")
    @PostMapping("/save")
    public Result save(@RequestBody CouponInfo couponInfo) {
        couponInfoService.saveCouponInfo(couponInfo);
        return Result.ok(null);
    }

    @ApiOperation("获取优惠券规则")
    @GetMapping("/findCouponRuleList/{id}")
    public Result findCouponRuleList(@PathVariable Long id) {
        Map<String, Object> map = couponInfoService.findCouponRuleList(id);
        return Result.ok(map);
    }

    @ApiOperation("新增优惠券规则")
    @PostMapping("/saveCouponRule")
    public Result saveCouponRule(@RequestBody CouponRuleVo couponRuleVo) {
        couponInfoService.saveCouponRule(couponRuleVo);
        return Result.ok(null);
    }
}

